package api

import (
	"gitee.com/keepalived_0807/Vblog/apps/blog"
	"gitee.com/keepalived_0807/Vblog/apps/token"
	"gitee.com/keepalived_0807/Vblog/common"
	"gitee.com/keepalived_0807/Vblog/exception"
	"gitee.com/keepalived_0807/Vblog/middleware"
	"gitee.com/keepalived_0807/Vblog/response"
	"github.com/gin-gonic/gin"
)

func (h *BlogApiHandler) Registry(appRouter gin.IRouter) {
	appRouter.GET("/", h.QueryBlogs)
	appRouter.GET("/:id", h.DescribeBlog)

	appRouter.Use(middleware.Auth)

	appRouter.POST("/", h.CreateBlog)
	appRouter.PUT("/:id", h.PutUpdateBlog)
	appRouter.PATCH("/:id", h.PatchUpdateBlog)
	appRouter.POST("/:id/status", h.UpdateBlogStatus)
	appRouter.DELETE("/:id", h.DeleteBlog)
}

// func (h *BlogApiHandler) auth(c *gin.Context) bool {
// 	tk, _ := c.Cookie(token.COOKIE_TOKEN_KEY)
// 	ioc.Controller.Get(token.AppName).(token.Service).ValidateToken(c.Request.Context(), token.NewValidateTokenRequest(tk))
// 	return true
// }

func (h *BlogApiHandler) QueryBlogs(c *gin.Context) {
	// 鉴权逻辑

	// if !h.auth(c) {
	// 	return
	// }

	// 获取 用户请求

	req := blog.NewQueryBlogsRequest()
	req.PageRequest = common.NewPageRequestFromCtx(c)
	req.Keywords = c.Query("keywords")
	// 处理 业务逻辑
	set, err := h.svc.QueryBlogs(c.Request.Context(), req)
	if err != nil {
		response.Failed(err, c)
		return
	}
	// 返回结果
	response.Success(set, c)
}

func (h *BlogApiHandler) DescribeBlog(c *gin.Context) {
	// 获取用户请求
	req := blog.NewDescribeBlogsRequest(c.Param("id"))
	// 处理业务逻辑
	ins, err := h.svc.DescribeBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(err, c)
		return
	}
	// 返回结果
	response.Success(ins, c)
}

func (h *BlogApiHandler) CreateBlog(c *gin.Context) {
	// 获取用户请求
	req := blog.NewCreateBlogRequest()
	if err := c.Bind(req); err != nil {
		response.Failed(exception.ErrValidateFailed(err.Error()), c)
		return
	}

	// 补充用户数据
	if v, ok := c.Get(token.GIN_TOKEN_KEY_NAME); ok {
		req.CreateBy = v.(*token.Token).Username
		if req.Author != "" {
			req.Author = req.CreateBy
		}
	}

	// 处理业务逻辑
	ins, err := h.svc.CreatedBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(err, c)
		return
	}
	// 返回结果
	response.Success(ins, c)
}

func (h *BlogApiHandler) PutUpdateBlog(c *gin.Context) {
	// 获取用户请求
	req := blog.NewUpdateBlogRequest(c.Param("id"))
	req.UpdateMode = common.UPDATE_MODE_PUT

	// body
	if err := c.Bind(req.CreatedBlogRequest); err != nil {
		response.Failed(exception.ErrValidateFailed(err.Error()), c)
		return
	}
	// 处理业务逻辑
	ins, err := h.svc.UpdateBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(err, c)
		return
	}
	// 返回结果
	response.Success(ins, c)
}

func (h *BlogApiHandler) PatchUpdateBlog(c *gin.Context) {
	req := blog.NewUpdateBlogRequest(c.Param("id"))
	req.UpdateMode = common.UPDATE_MODE_PATCH
	// body
	if err := c.Bind(req.CreatedBlogRequest); err != nil {
		response.Failed(exception.ErrValidateFailed(err.Error()), c)
		return
	}
	ins, err := h.svc.UpdateBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(err, c)
		return
	}
	response.Success(ins, c)
}

func (h *BlogApiHandler) DeleteBlog(c *gin.Context) {
	req := blog.NewDeleteBlogsRequest(c.Param("id"))
	ins, err := h.svc.DeleteBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(err, c)
		return
	}
	response.Success(ins, c)
}

func (h *BlogApiHandler) UpdateBlogStatus(c *gin.Context) {
	req := blog.NewUpdateBlogStatusRequest(c.Param("id"))
	if err := c.Bind(req.ChangedBlogStatusRequest); err != nil {
		response.Failed(exception.ErrValidateFailed(err.Error()), c)
		return
	}

	ins, err := h.svc.UpdateBlogStatus(c.Request.Context(), req)
	if err != nil {
		response.Failed(err, c)
		return
	}
	response.Success(ins, c)
}
